<?php

namespace App\Http\Controllers;

use App\Account;
use App\Repositories\EmailRepostory;
use App\WatchLog;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class EmailController extends Controller
{
    protected $email;

    public function __construct(EmailRepostory $email)
    {
        $this->email = $email;
        $this->user = \Auth::user();
    }

    public function collectionList()
    {
        $data = $this->email->collectionList();
        return view('email.collection',compact('data'));
    }

    public function show(Request $request,$messageid)
    {
        $data = $this->email->show($messageid);
        return view('email.show',compact('data'));
    }

    public function outboxList()
    {
        $data = $this->email->outboxList();
        return view('email.outbox',compact('data'));
    }

    public function getAdd(Request $request,$messageid,$fileName){
        $this->email->getAdd($messageid,$fileName);
    }

    public function write()
    {
        return view('email.write');
    }

    public function send(Request $request)
    {
        $res = $this->email->send($request);
    }

    public function index()
    {
        $emails = $this->email->pop3();
        dump($emails);
        return view('email.index',compact('emails'));
    }

    public function imgagesChange($_data)
    {
        $data = $_data;
        foreach ($_data['images'] as $k=>$image) {
            $data['body'] = str_replace('cid:'.$k,$image,$data['body']);
        }
        return $data;
    }

    public function emailPicture($images)
    {
        foreach ($images as &$image) {
            $image = $this->email->uploadQN($image);
        }
        return $images;
    }

    public function picture(Request $request)
    {
        $file = $request->file('myFileName');
        $filePath = $file->getRealPath();
        $key = rand(10,99).$file->getClientOriginalName();
        $file = $this->email->uploadQN($filePath,$key);
        return $file;
    }
}
